package com.darker.yygh.user.api;

import com.atguigu.yygh.model.user.Patient;
import com.darker.yygh.result.Result;
import com.darker.yygh.user.service.PatientApiService;
import com.darker.yygh.utils.AuthContextHolder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {

    @Autowired
    PatientApiService patientApiService;

    //获取就诊人列表
    @ApiOperation("获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAuthAll(HttpServletRequest request){
        //获取当前用户的Id
        Long userId = AuthContextHolder.getUserId(request);
       List<Patient> patientList=patientApiService.findAllByUserId(userId);
        return Result.ok(patientList);
    }
    //添加就诊人
    @ApiOperation("添加就诊人")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient,HttpServletRequest request){
        //获取当前用户的Id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientApiService.save(patient);
        return Result.ok();
    }
    //根据Id获取就诊人信息
    @ApiOperation("根据Id获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient=   patientApiService.getPatientById(id);
        return Result.ok(patient);
    }
    //修改就诊人
    @ApiOperation("修改就诊")
    @PutMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient){
        patientApiService.updateById(patient);
        return Result.ok();
    }
    //删除就诊人
    @ApiOperation("删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result removePatient(@PathVariable Long id){
        patientApiService.removeById(id);
        return Result.ok();
    }

    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable("id") Long id){
        Patient patient = patientApiService.getPatientById(id);

        return patient;
    }

}
